package com.snjgm.chapter1_alogimic;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.snjgm.chapter1_alogimic.util.DateUtil;

//发送消息端
public class SendActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_response;

    private static final String TAG = "sendActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        tv_response = findViewById(R.id.tv_response);
        findViewById(R.id.btn_send).setOnClickListener(this);

        //接收下级界面返回的数据
//        registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {
//           if (result!=null){
//               Intent intent = result.getData();
//               if(intent != null && result.getResultCode() == Activity.RESULT_OK){
//                   Bundle bundle = intent.getExtras();
//                   String responseTime = bundle.getString("response_time");
//                   String responseContent = bundle.getString("response_content");
//                   String rcvDesc = String.format("收到返回消息:+\n返回时间为%s\n返回内容为%s",responseTime,responseContent);
//                   tv_response.setText(rcvDesc);
//               }
//           }
//        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Log.d(TAG, "onActivityResult: resultCode "+resultCode);

        if(intent != null && resultCode == Activity.RESULT_OK){
            Bundle bundle = intent.getExtras();
            String responseTime = bundle.getString("response_time");
            String responseContent = bundle.getString("response_content");
            String rcvDesc = String.format("收到返回消息:+\n返回时间为%s\n返回内容为%s",responseTime,responseContent);
            tv_response.setText(rcvDesc);
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, ReciveActivity.class);
        Bundle bundle = new Bundle();
        Log.d(TAG,"onclick data: "+ DateUtil.getNowDate());
        bundle.putString("request_time", DateUtil.getNowDate());
        bundle.putString("request_place","上海");
        intent.putExtras(bundle);
        startActivityForResult(intent,1);
        //startActivity(intent);
    }
}